﻿<?php
////////////////配置文件
$dbhost = 'localhost';
$dbuser = 'xuzp'; 
$dbpass = 'xuzp580208';    
$dbname = 'cvh';
$main_table='cvh_hgas';
$title='贵州科学院';
///////////////////////////////////////
$connection = mysql_connect ($dbhost, $dbuser, $dbpass);
if (!$connection) {  die('没有连接: ' . mysql_error());}
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {  die ('不能使用数据库 : ' . mysql_error());}
/*
$connection = mysql_connect ("localhost", "root", "");
if (!$connection) {  die('没有连接: ' . mysql_error());}
$db_selected = mysql_select_db("cvh", $connection);
if (!$db_selected) {  die ('不能使用数据库 : ' . mysql_error());}
*/
if(isset($_GET['fid']))
{
$lsid=explode(":",$_GET['fid']);  //分解KML文件参数，得到第二个实际要用的参数

$query ="SELECT ScientificName,County,DecimalLongitude,DecimalLatitude,count(GlobalUniqueIdentifier) cc from ".$main_table.
       " where LSID='".$lsid[1]."' and length(DecimalLatitude)>2 group by LSID";
mysql_query("set names 'utf8'");	   
$result = mysql_query($query);
 if (!$result) 
 {  die('无效检索语句: ' . mysql_error()); }
 
if($_GET['format']=='xml'){
$kml ='<?xml version="1.0" encoding="UTF-8" ?><marks>';
while ($row = @mysql_fetch_assoc($result)) 
  $kml.= '<marker sname="'.$row['ScientificName'].'" '.
        'lat="'.$row['DecimalLatitude'].'" lng="'.$row['DecimalLongitude'].'" '.
		'county="'.$row['County'].'" count="'.$row['cc'].'"/> ';
$kml.= '</marks>';
header("Content-type: text/xml");
echo $kml;
}

else if($_GET['format']=='kml'){
$kml = array('<?xml version="1.0" encoding="UTF-8"?>');
$kml[] = '<kml xmlns="http://earth.google.com/kml/2.2">';
$kml[] = '<Document>';

while ($row = @mysql_fetch_assoc($result)) 
{
$kml[] = '<Placemark>';
$kml[] = '<name>'.htmlentities($row['ScientificName']).'</name>';
$kml[] = '<description>县名：'.$row['County'].'，数量：'.$row['cc'].'&lt;br&gt;版权所有：'.$title.'</description>';
$kml[] = '<Point>';
$kml[] = '<coordinates>'.$row['DecimalLongitude'].',' . $row['DecimalLatitude'] .',0</coordinates>';
$kml[] = '</Point>';
$kml[] = '</Placemark>'; 
}
$kml[] = '</Document>';
$kml[] = '</kml>';
$kmlOutput = join("\n", $kml);
header('Content-type: application/vnd.google-earth.kml+xml');
header('Content-Disposition: attachment; filename="cvh.kml"');
echo $kmlOutput;
}

}
else ;
?>